/*
 * campfire, formerly known as bandcampDirect
 * Copyright (C) 2020 Fynn Godau
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Not affiliated with bandcamp, Incorporated.
 */

package godau.fynn.bandcampdirect.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import godau.fynn.bandcampdirect.R;
import godau.fynn.bandcampdirect.SharedPreferences;
import godau.fynn.bandcampdirect.adapter.SearchResultAdapter;
import godau.fynn.bandcampdirect.bandcamp.Bandcamp;
import godau.fynn.bandcampdirect.bandcamp.Downloader;
import godau.fynn.bandcampdirect.extractor.converter.SearchConverter;
import godau.fynn.bandcampdirect.model.RowViewDisplayable;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends Activity {

    public static final String EXTRA_QUERY = "query";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        SharedPreferences sharedPreferences = new SharedPreferences(SearchActivity.this);
        Bandcamp bandcamp = new Bandcamp(sharedPreferences.createUser());

        NewPipe.init(new Downloader(null));


        String query = getIntent().getStringExtra(EXTRA_QUERY);

        setTitle("searching " + query + " | campfire");

        TextView status = findViewById(R.id.status);

        status.setText("Searching");

        new Thread(() -> {

            try {
                List<InfoItem> searchResults = ServiceList.Bandcamp.getSearchExtractor(query)
                        .getInitialPage().getItems();

                runOnUiThread(() -> {

                    ArrayList<RowViewDisplayable> rowViewDisplayables = new ArrayList<>();

                    for (InfoItem result : searchResults) {
                        rowViewDisplayables.add(SearchConverter.toRowViewDisplayable(result));
                    }

                    status.setVisibility(View.GONE);

                    // Display results

                    RecyclerView recyclerView = findViewById(R.id.results);
                    recyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));

                    recyclerView.setAdapter(new SearchResultAdapter(rowViewDisplayables));

                    // User might click something
                    setResult(RESULT_OK);
                });
            } catch (ExtractionException | IOException e) {
                runOnUiThread(() -> status.setText(
                        e instanceof ExtractionException? "Search is broken, sorry" : "Network issue"
                ));
            }
        }).start();
    }
}
